梦入琼楼寒有月,行过石树冻无烟

PHP 文件指针

在PHP开发语言之中,共支持feof()、rewind()、ftell()、fseek()等函数进行文件指针操作

判断指针是否处于文件EOF

feof
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
$file = "D:/Program Files (x86)/phpStudy/WWW/text/1.txt";
$hand = fopen($file, "r");

// 重置指针位置
rewind($hand);

if (feof($hand)) {
echo "当前指针处于文件尾部";
} else {
echo "当前指针不处于指针尾部,处于:",ftell($hand);
}
?>
</body>
</html>

当前指针位置

ftll()

feof()函数主要用于测试文件指针是否处于EOF;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
$file = "D:/Program Files (x86)/phpStudy/WWW/text/1.txt";
$hand = fopen($file, "r");
// 输出当前指针位置
echo "当前指针为:",ftell($hand)."<br>";

?>
</body>
</html>

挪动指针位置

fseek()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
$file = "D:/Program Files (x86)/phpStudy/WWW/text/1.txt";
$hand = fopen($file, "r");

// 将指针挪动2
fseek($hand, 2);
echo "当前指针为:",ftell($hand)."<br>";

?>
</body>
</html>

重置指针位置

rewind()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
$file = "D:/Program Files (x86)/phpStudy/WWW/text/1.txt";
$hand = fopen($file, "r");

// 重置指针位置
rewind($hand);
echo "当前指针为:",ftell($hand)."<br>";
?>
</body>
</html>
⬅️ Go back